package com.controller;

import com.constant.Constant;
import com.entity.GoldPoints;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import com.util.AjaxResult;
import com.util.CommonUtil;
import com.vo.UserBackVo;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * date:2021/5/6
 * description
 * Author:lfz
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;



    //    /user/getCode.do
    @RequestMapping("/getCode.do")
    @ResponseBody
    public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("UserController.getCode()");
        CommonUtil.MyImage m = CommonUtil.getImage(null, 4, true, true);
        System.out.println("code=" + m.getCode());
        request.getSession().setAttribute("code", m.getCode());
        ServletOutputStream responseOutputStream = response.getOutputStream();
        // 输出图象到页面
        ImageIO.write(m.getImage(), "JPEG", responseOutputStream);
        // 以下关闭输入流！
        responseOutputStream.flush();
        responseOutputStream.close();
    }

    @RequestMapping("/loginBack.do")
    @ResponseBody
    public AjaxResult loginBack(HttpServletRequest request, User u) {
        AjaxResult result = new AjaxResult();
        String code =  request.getParameter("code");
        String localCode = (String) request.getSession().getAttribute("code");
        if(localCode ==null || !localCode.equals(code)){
            result.setMsg("验证码错误");
            //return result;
        }

        System.out.println("controller:"+u);
        try {
            User user = userService.loginBack(u);
            request.getSession().setAttribute(Constant.SESSION_USER, user);
            result.setSuccess(true);
        }catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("/findBackUser.do")
    @ResponseBody
    public PageInfo<UserBackVo> findBackUser(HttpServletRequest request,UserBackVo userBackVo )
    {
        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        PageInfo<UserBackVo> info = userService.findBackUser(pageNo,userBackVo);
        return info;
    }

//    modifyBackUser.do
//    user/showBackUser.do

    @RequestMapping("/showBackUser.do")
    @ResponseBody
    public AjaxResult showBackUser(HttpServletRequest request){

        Integer id=Integer.valueOf(request.getParameter("id"));
//        userService.showBackUser(id);
        return userService.showBackUser(id);
    }

    //    modifyBackUser.do
    @RequestMapping("/modifyBackUser.do")
    @ResponseBody
    public Integer modifyBackUser(HttpServletRequest request){
        User user=new User();
        Integer id= Integer.valueOf(request.getParameter("id"));
        user.setId(id);
        user.setEmail(request.getParameter("email"));
        user.setNickname(request.getParameter("nickname"));
        user.setPassword(request.getParameter("password"));
        user.setRole(request.getParameter("role"));
        userService.modifyBackUser(user);
        System.out.println(user);
        return 0;
    }

    @RequestMapping("/modifyBackUserS.do")
    @ResponseBody
    public User modifyBackUserS(HttpServletRequest request){
        User user=new User();
        Integer id= Integer.valueOf(request.getParameter("id"));
        Integer status= Integer.valueOf(request.getParameter("status"));
        user.setId(id);
        user.setStatus(status);
        userService.modifyBackUserS(user);
        return user;
    }

    @RequestMapping("/backloginOut.do")
    @ResponseBody
    public  String backloginOut(){
        return "/backlogin";
    }



    //罗前台
//    /user/check.do',
    @RequestMapping("/check.do")
    @ResponseBody
    public AjaxResult check(String login_name){
        AjaxResult ajaxResult=new AjaxResult();
        List<User> users=userService.check(login_name);
        if (users.size()==0){
            ajaxResult.setMsg("用户名正确");
            ajaxResult.setSuccess(true);
            return ajaxResult;
        }
        ajaxResult.setMsg("用户名重复");
        return ajaxResult;
    }
//    /user/checkEmail
    @RequestMapping("/checkEmail.do")
    @ResponseBody
    public AjaxResult checkEmail(String email){
        AjaxResult ajaxResult=new AjaxResult();
        List<User> users=userService.checkEmail(email);
        if (users.size()==0){
            ajaxResult.setMsg("邮箱正确");
            ajaxResult.setSuccess(false);
            return ajaxResult;
        }
        ajaxResult.setMsg("邮箱重复");
        return ajaxResult;
    }
//    /user/loginFront.do',
    @RequestMapping("/loginFront.do")
    @ResponseBody
    public AjaxResult loginFront(HttpServletRequest request,User u){
        AjaxResult result = new AjaxResult();
        String code =  request.getParameter("code");
        String localCode = (String) request.getSession().getAttribute("code");
        if(localCode ==null || !localCode.equals(code)){
            result.setMsg("验证码错误");
            //return result;
        }
        System.out.println("controller:"+u);
        try {
            User user = userService.loginFront(u);
            request.getSession().setAttribute(Constant.SESSION_USER, user);
            result.setSuccess(true);
        }catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
//    /user/regist.do'
    @RequestMapping("/regist.do")
    @ResponseBody
    public AjaxResult regist(User u,HttpServletRequest request){
        AjaxResult ajaxResult=new AjaxResult();
        try {
            User user = userService.regist(u);
//            ajaxResult.setObj(user);
            request.getSession().setAttribute(Constant.SESSION_USER, user);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }
//    /user/loginOut1.do
    @RequestMapping("/loginOut1.do")
    public String loginOut1(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:/login";
    }

//    /user/modifyUser.do'
    @RequestMapping("/modifyUser.do")
    @ResponseBody
    public AjaxResult modifyUser(HttpServletRequest request){
        String newPassword = request.getParameter("newPassword");
        Integer id=Integer.valueOf(request.getParameter("id"));
        String nickname=request.getParameter("nickname");
        String email=request.getParameter("email");
        System.out.println(id);
        AjaxResult ajaxResult=new AjaxResult();
        ajaxResult.setMsg(userService.modifyUser(id,nickname,email,newPassword));
        return ajaxResult;
    }
//    /user/updateLoginDate.do',
    @RequestMapping("/updateLoginDate.do")
    @ResponseBody
    public Integer updateLoginDate(Integer id){
        System.out.println(id);
        try {
            GoldPoints goldPoints = new GoldPoints();
            goldPoints.setUser_id(id);
            goldPoints.setPoint_count(5);
            goldPoints.setInfo("签到");
            goldPoints.setCreate_date(new Date());
            userService.updateLoginDate(goldPoints);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
//    /user/findLoginDate.do',
    @RequestMapping("/findLoginDate.do")
    @ResponseBody
    public Boolean findLoginDate(HttpServletRequest request){
        try {
            User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
            Date date=userService.findLoginDate(user.getId()).getCreate_date();
            if (date == null) {
                return true;
            }
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE, 1);
            date = calendar.getTime();
            Date date1 = new Date();
            if (date.getTime() > date1.getTime()) {
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }
}
